//
//  AppDelegate.m
//  McRemoteServer
//
//  Created by Magic on 12/6/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"
#import "MRServer.h"

#import "MREventCenter.h"
#import "MRMessageCenter.h"
#import "DockManager.h"

@implementation AppDelegate {
    NSStatusItem *_statusItem;

}

@synthesize window = _window;
@synthesize menu = _menu;
@synthesize clientInfoItem = _clientInfoItem;

- (void)dealloc
{
    [_statusItem release];
    [super dealloc];
}

- (void)setupStatusItem
{
    _statusItem = [[[NSStatusBar systemStatusBar] 
                    statusItemWithLength:NSVariableStatusItemLength]
                   retain];
    [_statusItem setHighlightMode:YES];
    
    [_statusItem setImage:[NSImage imageNamed:@"icon 18.png"]];
    [_statusItem setAlternateImage:[NSImage imageNamed:@"icon 18 white.png"]];                  
    
    [_statusItem setMenu:self.menu];


}



- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    [self setupStatusItem];
    
    [[MRServer sharedServer] start];
    [[MRServer sharedServer] enableBonjourWithServiceType:@"mcremote"];
    
    [[MRServer sharedServer] setDelegate:[MRMessageCenter sharedCenter]];
    
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(active:) name:NSWorkspaceDidActivateApplicationNotification object:nil];
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(didLaunchApplication:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(didTerminateApplication:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onClientDeviceName:) name:@"ClientDeviceName" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNewConnection:) name:@"NewConnection" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onConnectionLost:) name:@"ConnectionLost" object:nil];
}

- (void)didTerminateApplication:(NSNotification *)noti
{
    NSRunningApplication *app = [noti.userInfo objectForKey:NSWorkspaceApplicationKey];
    NSString *appName = app.localizedName;
    [[DockManager sharedManager] terminateAppNamed:appName];
    [[MRMessageCenter sharedCenter] sendTerminateApplication:appName];
}

- (void)didLaunchApplication:(NSNotification *)noti
{
    NSRunningApplication *app = [noti.userInfo objectForKey:NSWorkspaceApplicationKey];
    NSString *appName = app.localizedName;
    NSLog(@"launch:%@",appName);
    
    [[DockManager sharedManager] setDockAppName:appName instance:app];
    
    [[MRMessageCenter sharedCenter] sendLaunchApplication:appName];
}

- (void)active:(NSNotification *)noti
{    
    NSRunningApplication *app = [noti.userInfo objectForKey:NSWorkspaceApplicationKey];
    
    NSLog(@"activate:%@",app.localizedName);
    
    [[MRMessageCenter sharedCenter] sendActiveApplication:app.localizedName];
}

- (void)onNewConnection:(NSNotification *)notification
{
    NSLog(@"connected");
    [_statusItem setImage:[NSImage imageNamed:@"icon 18 blue.png"]];
}

- (void)onConnectionLost:(NSNotification *)notification
{
    NSLog(@"disconnected");
    self.clientInfoItem.title = @"No connection";
    [_statusItem setImage:[NSImage imageNamed:@"icon 18.png"]];
}

- (void)onClientDeviceName:(NSNotification *)notification
{
    self.clientInfoItem.title = [notification.userInfo objectForKey:@"name"];
}

- (IBAction)onClickClientInfoItem:(id)sender
{
    //does nothing
}

@end
